# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: pyppeteer_tb.py
@time: 2021/9/29 3:35
'''
import asyncio
from icecream import ic
from pyppeteer import launch

url = 'https://www.taobao.com/'

async def load_cookie():
    b = 'cna=w6qoGSxtszoCAXjlXYAYXnPP; _m_h5_tk=5805fbad31d558aa9812d124c23661ae_1632853636509; _m_h5_tk_enc=63f2804456202714670b2244202a0960; xlly_s=1; _samesite_flag_=true; cookie2=1a85ed2929560ef4c15f254395ea47ec; t=9a8a6143ce5b71485769ebf619058930; _tb_token_=e3e3e16e0e3ea; sgcookie=E100wc7SgHau0uMGa2r%2BGubOE%2BOH7nFfs6jG%2BSMCN0jERo%2BddzIzqn34fdMsCFbEbbsu9yToImNBydyIWHzNb3h8eCm%2FPb2yrdx3EwcQ%2F%2BtA%2FVI%3D; unb=33387488; uc3=id2=UNN%2BznzMzfI%3D&nk2=DMH3VvlZHk%2Bsn4Np&vt3=F8dCujaPnnc9Bh6xVAE%3D&lg2=U%2BGCWk%2F75gdr5Q%3D%3D; csg=91ab5a33; lgc=operationers; cancelledSubSites=empty; cookie17=UNN%2BznzMzfI%3D; dnk=operationers; skt=d4dfe0efb328e16f; existShop=MTYzMjg0MzI2NQ%3D%3D; uc4=nk4=0%40DtZFh4WhOEH4RO9xWBcYCMiFa6gIlco%3D&id4=0%40UgQ2iGzNkrLs1kqEgDTTQqNszA%3D%3D; publishItemObj=Ng%3D%3D; tracknick=operationers; _cc_=URm48syIZQ%3D%3D; _l_g_=Ug%3D%3D; sg=s85; _nk_=operationers; cookie1=BYfuCYWgNiO%2BOaWyj%2BN19MjSoGWtJ2H4JzKlzl%2Bwcnk%3D; enc=73p7JT3qq4T5Nm8oLWVnPR%2FHmBuzIzHTXCgUfBKQRnGE8S9Y5Bwl2m2h5JCxMpWmOupZWOJm6%2Bn9cLzDRqKwfA%3D%3D; JSESSIONID=A6A0A45935FEA7CB88948C8AB0AA5A78; hng=CN%7Czh-CN%7CCNY%7C156; thw=cn; mt=ci=93_1; uc1=existShop=false&cookie14=Uoe3dYldoqlRwQ%3D%3D&cookie15=Vq8l%2BKCLz3%2F65A%3D%3D&pas=0&cookie16=WqG3DMC9UpAPBHGz5QBErFxlCA%3D%3D&cookie21=V32FPkk%2Fhodroid0QSjisQ%3D%3D; tfstk=cMpFBmqNOvHFvzBSM96rF6Az5vIdZUxHqRSftBAVkMISeKChi0zRjDAcbg_xI6f..; l=eB_gSagggERJ-asDBO5aourza7796IRb4sPzaNbMiInca6iR1Fsg3NCLnu6eRdtjgtCEoeKrwmwghRLHR3AiVh9N33h2q_yE3xvO.; isg=BKamCSHU-2LaKK-0fHzTXD0w9xwoh-pBxe4mm5BPZkmkE0Yt-RZGUYbpaw-fu-JZ'
    list_cookie = []
    for line in b.split(';'):
        key, value = line.split('=', 1)
        list_cookie.append({'name': key.split()[0], 'value': value.split()[0]})
    return list_cookie


# 加载首页
# async def index(page,cookies):
async  def run():
    browser = await launch({'headless': False,
                            'dumpio': True,
                            'args': [
                                # '--disable-extensions',
                                # '--disable-bundled-ppapi-flash',
                                # '--mute-audio',
                                # '--no-sandbox',
                                # '--disable-setuid-sandbox',
                                '--disable-dev-shm-usage',
                                '--shm-size=1gb'
                                '--disable-gpu',
                            ]
                            })
    page = await browser.newPage()
    await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36')

    cookies = await load_cookie()
    await page.setViewport({'width': 1366, 'height': 768})
    await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                            'Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299')
    dimensions = await page.evaluate('''() => { return { cookie: window.document.cookie, } }''')
    await page.goto('url')
    for cookie in cookies:
        await page.setCookie(cookie)
    await page.goto('url')
    ic("登录成功")
    await page.waitForSelector('.logged', {'timeout': 300000})
    await asyncio.sleep(60)
    cookies2 = await page.cookies()
    ic(cookies2)



if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(run())